// Ofri Keidar 302933981 group 05

package builder;

import controller.AdministratorController;
import controller.CmnTransactionsCtrl;
import controller.CommonValues;
import controller.CustomerController;
import controller.EmployeeController;
import controller.ManagerController;
import controller.SystemController;
import model.Model;
import model.SystemDataManager;
import model.database.DBException;
import model.database.Database;
import model.database.StoreDB;

/**
 * Class uses to build the controller matching user's permission
 */
public class ControllerBuilder {

	private SystemController controller; // used to perform transactions
	
	/**
	 * Constructor- creates a new builder object
	 * @throws DBException- if had problems initializing system's components
	 * 					    that regard the database
	 */
	public ControllerBuilder() throws DBException {
		
		// create database
		Database database = new StoreDB();
		
		// create model
		Model model = new SystemDataManager(database);
		
		// create controller
		controller = new SystemController(model); 
	}
	
	/**
	 * Gets user-name and password and returns user's permission.
	 * If details are not correct (no such user or wrong password),
	 * return null;
	 * @param userName- user-name to check
	 * @param password- user's password
	 * @return user's permission or null if wrong details
	 * @throws DBException if had problems connecting to database
	 */
	public CommonValues.permissions getUserPermission(String userName,
			String password) throws DBException {
		
		// return user's permission
		return controller.getUserPermission(userName, password);
	}
	
	/**
	 * Returns controller-view for transactions that are common to all
	 * user profiles
	 * @return system's controller through a common-controller view 
	 */
	public CmnTransactionsCtrl getCommonController() { return controller; }
	
	/**
	 * Returns controller-view for an administrator user
	 * @return system's controller through an administrator view 
	 */
	public AdministratorController getAdminController() { return controller; }
	
	/**
	 * Returns controller-view for a customer user
	 * @return system's controller through a customer view 
	 */
	public CustomerController getCustomerController() { return controller; }
	
	/**
	 * Returns controller-view for an employee user
	 * @return system's controller through an employee view 
	 */
	public EmployeeController getEmployeeController() { return controller; }
	
	/**
	 * Returns controller-view for a manager user
	 * @return system's controller through a manager view 
	 */
	public ManagerController getManagerController() { return controller; }
}
